package com.bj58.move;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class CateMoveExcel2Java{

	private String excelPath;
	private String outputPath;

	public CateMoveExcel2Java( String excelPath, String outputPath ){

		this.excelPath = excelPath;
		this.outputPath = outputPath;
	}

	public void generateJava(){

		if( excelPath != null && outputPath != null ){

			String template = readTemplate();
			StringBuilder builder = new StringBuilder();
			Map< String, String > check = new HashMap< String, String >();
			Map< String, String > secondCate = new HashMap< String, String >();

			try{
				Workbook workbook = Workbook.getWorkbook( new File( excelPath ) );
				Sheet sheet = workbook.getSheet( 0 );

				for( int i = 2; i < sheet.getRows(); i++ ){

					Cell[] row = sheet.getRow( i );

					String instanceString = row[ 3 ].getContents();
					String listname = row[ 6 ].getContents().trim();

					int instance = tryParseInt( instanceString );
					if( instance > -1 && !listname.startsWith( "qz" ) ){

						String oldSecondCateId = row[ 0 ].getContents().trim();
						String param = row[ 2 ].getContents().trim();
						String thirdCateId = row[ 4 ].getContents().trim();
						String thirdDisplyCateId = row[ 1 ].getContents().trim();

						if( check.get( thirdCateId ) != null ){
							throw new Exception( thirdCateId + ":已经存在" );
						}

						builder.append( "cmc2xiaozhiMap.put(" );
						builder.append( thirdCateId );
						builder.append( "," );
						builder.append( "\"cateid=" );
						builder.append( oldSecondCateId );
						builder.append( "&param" );
						builder.append( param );
						builder.append( "=" );
						builder.append( instance );
						builder.append( "\");" );

						builder.append( "xiaozhiwei2cateMap.put(" );
						builder.append( "\"cateid=" );
						builder.append( oldSecondCateId );
						builder.append( "&xiaozhiwei" );
						builder.append( "=" );
						builder.append( instance );
						builder.append( "\"," );
						builder.append( thirdCateId );
						builder.append( ");" );

						builder.append( "xiaozhiwei2displayMap.put(" );
						builder.append( "\"cateid=" );
						builder.append( oldSecondCateId );
						builder.append( "&xiaozhiwei" );
						builder.append( "=" );
						builder.append( instance );
						builder.append( "\"," );
						builder.append( thirdDisplyCateId );
						builder.append( ");" );

						if( secondCate.get( oldSecondCateId ) == null ){

							builder.append( "second2Xiaozhiwei.put(" );
							builder.append( oldSecondCateId );
							builder.append( "," );
							builder.append( param );
							builder.append( ");" );
							
							secondCate.put( oldSecondCateId, oldSecondCateId );
						}

						check.put( thirdCateId, "ok" );
					}
				}

				template = template.replace( "/** replace **/", builder.toString() );
				write( template );
			}
			catch( Exception ex ){
				ex.printStackTrace();
			}
		}
	}

	private int tryParseInt( String instance ){

		try{
			int out = Integer.parseInt( instance );
			return out;
		}
		catch( Exception ex ){
			return -1;
		}
	}

	private void write( String content ){

		try{
			File file = new File( this.outputPath );
			if( !file.exists() ){
				if( file.createNewFile() ){
					System.out.println( "文件创建成功！" );
				}
				else{
					System.out.println( "文件创建失败！" );
				}
			}

			content = content.replace( "Cate2XiaozhiweiMapperTemplate", file.getName().substring( 0, file.getName().indexOf( "." ) ) );

			BufferedWriter output = new BufferedWriter( new FileWriter( file ) );
			output.write( content );
			output.close();
		}
		catch( Exception e ){
			e.printStackTrace();
		}
	}

	private String readTemplate(){

		String file = "D:\\eclipse4.2.2\\workspace\\com.bj58.move\\src\\main\\java\\com\\bj58\\move\\Cate2XiaozhiweiMapperTemplate.java";
		StringBuilder sb = new StringBuilder();
		String s = null;

		File f = new File( file );
		if( f.exists() ){
			try{
				BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream( f ) ) );
				while( ( s = br.readLine() ) != null ){
					sb.append( s );
				}
			}
			catch( Exception e ){
				e.printStackTrace();
			}
		}

		return sb.toString();
	}

	// ---------------------------------------------------- Get/Set Methods

	public String getExcelPath(){

		return excelPath;
	}

	public String getOutputPath(){

		return outputPath;
	}
}
